package com.qingdai.controller;

import com.github.yulichang.query.MPJQueryWrapper;
import com.qingdai.annotate.IgnoreJwtCheck;
import com.qingdai.emuns.Address;
import com.qingdai.emuns.AddressDTO;
import com.qingdai.entities.CommonResult;
import com.qingdai.service.AddressEsService;
import com.qingdai.service.AddressService;
import com.qingdai.service.impl.AddressEsServiceImpl;
import com.qingdai.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 详细地址 前端控制器
 * </p>
 *
 * @author LiuZiMing
 * @since 2024-01-26
 */
@RestController
@RequestMapping("/address")
@Tag(name = "详细地址")
public class AddressController {
    @Resource
    private AddressService addressService;
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private AddressEsService addressEsService;

    @PostMapping("/es/xyz")
    @Operation(summary = "所有地址存到ES", description = "")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
    })
    private CommonResult<List<Address>> addAddressesToES() {
        List<Address> allAddress = addressService.list();
        Iterable<Address> addressesIterable = addressEsService.saveAddresses(allAddress);
        return new CommonResult<>(200, "所有地址存到ES成功");
    }

    @GetMapping("/es/id/xyz")
    @Operation(summary = "根据id从ES获取地址", description = "")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    private CommonResult<Address> getAddressByIdFromES(Integer id) {
        Address addressById = addressEsService.getAddressById(id);
        return new CommonResult<>(200, "根据id从ES获取地址成功", addressById);
    }

    @GetMapping("/es/id2")
    @Operation(summary = "根据id从ES获取地址DTO", description = "")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    private CommonResult<AddressDTO> getAddressDTOByIdFromES(Integer id) {
        AddressDTO addressDTOById = addressEsService.getAddressDTOById(id);
        return new CommonResult<>(200, "根据id从ES获取地址DTO成功", addressDTOById);
    }

    @GetMapping("/xyz")
    @Operation(summary = "获取所有创建地址", description = "获取所有创建地址")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
    })
    private CommonResult<List<Address>> getAddress() {
        List<Address> list = addressService.list();
        return new CommonResult<>(200, "获取所有地址成功", list);
    }

    @GetMapping("/jwt")
    @Operation(summary = "获取本人创建地址", description = "获取本人创建地址")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
    })
    private CommonResult<List<Address>> getAddressByJwt(HttpServletRequest request) {
        CommonResult userIdByJwtCommonResult = jwtUtil.getUserIdByJwt(request);
        Integer userIdByJwt = (Integer) userIdByJwtCommonResult.getData();
        MPJQueryWrapper mpjQueryWrapper = new MPJQueryWrapper();
        mpjQueryWrapper.select("*").eq("user_id", userIdByJwt);
        List<Address> list = addressService.list(mpjQueryWrapper);
        return new CommonResult<>(200, "获取本人所有地址成功", list);
    }

    @PostMapping("/jwt")
    @Operation(summary = "创建新地址", description = "创建新地址")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "name", description = "昵称", required = true),
            @Parameter(name = "provinceId", description = "省名", required = true),
            @Parameter(name = "cityId", description = "市名", required = true),
            @Parameter(name = "districtId", description = "区名", required = true),
            @Parameter(name = "position", description = "位置", required = true),
            @Parameter(name = "phoneNumber", description = "手机号", required = true),

    })
    public CommonResult createAddressByJwt(@RequestParam(required = true) String name,
                                           @RequestParam(required = true) Integer provinceId,
                                           @RequestParam(required = true) Integer cityId,
                                           @RequestParam(required = true) Integer districtId,
                                           @RequestParam(required = true) String position,
                                           @RequestParam(required = true) Long phoneNumber,
                                           HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByJwt(request);
        Integer jwtUserId = (Integer) result.getData();
        boolean createResult = addressService.save(new Address(null, name, provinceId, cityId, districtId, position, phoneNumber, jwtUserId));
        if (createResult) {
            return new CommonResult(200, "创建地址成功");
        } else {
            return new CommonResult(400, "创建地址失败");
        }
    }

    @PutMapping("/jwt/id")
    @Operation(summary = "根据id更改地址", description = "根据id更改地址")
    @Parameters({@Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "name", description = "昵称", required = false),
            @Parameter(name = "provinceId", description = "省名", required = false),
            @Parameter(name = "cityId", description = "市名", required = false),
            @Parameter(name = "districtId", description = "区名", required = false),
            @Parameter(name = "position", description = "位置", required = false),
            @Parameter(name = "phoneNumber", description = "手机号", required = false),

    })
    public CommonResult updateAddressByIdAndJwt(@RequestParam(required = true) Integer id,
                                                @RequestParam(required = false) String name,
                                                @RequestParam(required = false) Integer provinceId,
                                                @RequestParam(required = false) Integer cityId,
                                                @RequestParam(required = false) Integer districtId,
                                                @RequestParam(required = false) String position,
                                                @RequestParam(required = false) Long phoneNumber,
                                                HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByJwt(request);
        Integer jwtUserId = (Integer) result.getData();
        Address address = addressService.getById(id);
        if (address == null) {
            return new CommonResult(400, "id非法");
        } else if (Objects.equals(address.getUserId(), jwtUserId)) {
            boolean createResult = addressService.saveOrUpdate(new Address(id, name, provinceId, cityId, districtId, position, phoneNumber, jwtUserId));
            if (createResult) {
                return new CommonResult(200, "修改地址成功");
            } else {
                return new CommonResult(400, "修改地址失败");
            }
        } else {
            return new CommonResult(400, "请勿修改他人地址");
        }
    }

    @DeleteMapping("/jwt/id")
    @Operation(summary = "根据id删除地址", description = "根据id删除地址")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult deleteAddressByIdAndJwt(@RequestParam(required = true) Integer id,
                                                HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByJwt(request);
        Integer jwtUserId = (Integer) result.getData();
        Address address = addressService.getById(id);
        if (address == null) {
            return new CommonResult(400, "id非法");
        } else if (Objects.equals(address.getUserId(), jwtUserId)) {
            boolean createResult = addressService.removeById(id);
            if (createResult) {
                return new CommonResult(200, "删除地址成功");
            } else {
                return new CommonResult(400, "删除地址失败");
            }
        } else {
            return new CommonResult(400, "请勿删除他人地址");
        }
    }
}
